import { Component, OnInit } from '@angular/core';
import { Message } from 'primeng/api';
import { ActivatedRoute, Router } from '@angular/router';
import { ProjectService } from '../service/project.service';
import { MessageUtilsService } from '../../shared/services/message-utils.service';
import * as _ from 'lodash';
import { SaleNotificationService } from '../service/sale-notification.service';
import { CRUD_MODE, UserRoles } from '../../shared/config/app-settings';
@Component({
  selector: 'app-sale-notification',
  templateUrl: './sale-notification.component.html',
  styleUrls: ['./sale-notification.component.scss']
})
export class SaleNotificationComponent implements OnInit {
  msgs: Message[] = [];
  display :boolean = false;
  //p-table
  dataSource:any[];
  selectedColumns: any[];
  cols: any[];
  comments:string;
  currentNotification:any = {};
  constructor(private route:ActivatedRoute,
    private router:Router,
    private saleNotificationService:SaleNotificationService,
    private projectService:ProjectService,
    private messageUtilsService:MessageUtilsService) { }

  ngOnInit() {
    this.cols = [
      { field: 'id', header: 'ID' },
      { field: '_projectName', header: 'PROJECT' },
      { field: '_lot', header: 'PROPERTY' },
      { field: '_code', header: 'iPAN Member ID' },
      { field: '_time', header: 'Submit Time' },
      { field: 'saleNotificationContent', header: 'Sale Notes' }
    ];
    this.selectedColumns = _.cloneDeep(this.cols); //toggle cols
    this.selectedColumns.splice(0,1); //default hide first column
    this.loadAll();
  }

  loadAll(){
    this.saleNotificationService.getAll({}).subscribe(
      res=>{
        this.dataSource = res;
        _.each(res, (note)=>this.enrichResponse(note));
      }
    )
    
  }

  viewProject(rowData){
    this.router.navigate(['../project-detail/'+rowData.property.propertyList.id], { relativeTo: this.route, queryParams: { mode: CRUD_MODE.VIEW } });
  }

  viewProperty(rowData){
    this.router.navigate(['../property-detail/' + rowData.propertyId], { relativeTo: this.route, queryParams: { menuKey: 'house-detail', mode: CRUD_MODE.VIEW } });
  }

  viewAgent(rowData){
    this.router.navigate(['/admin/shared/profile/',rowData.agentId],  { relativeTo: this.route, queryParams: { role: UserRoles.AGENT, mode:CRUD_MODE.VIEW } });
  }

  viewNotes(rowData){
    this.currentNotification = rowData;
    console.log(rowData)
    this.comments = "";
    this.display = true;
  }

  approve(){
    this.saleNotificationService.approve(this.currentNotification.id, {comments:this.comments}).subscribe(res=>{
      console.log(res);
      this.loadAll();
    });
    this.display = false;

  }

  reject(){
    this.saleNotificationService.reject(this.currentNotification.id, {comments:this.comments}).subscribe(res=>{
      console.log(res);
      this.loadAll();
    });
    this.display = false;

  }

  enrichResponse(note){
    note._projectName = note.property.propertyList.name;
    note._lot = note.property.lot;
    note._code = note.agent.code;
    note._time = new Date(note.submitNotifySaleTime);
  }

}
